// Garima Aggarwal
#include "GUIKitRobot.h"
#include <qpainter.h>
#include <QTimer.h>
#include <cmath>
#include <QHBoxLayout>
#include <qpushbutton.h>
#include <qkeyeventtransition.h>
#include <QKeyEvent>
#include <qmath.h>

// constructor
GUIKitRobot::GUIKitRobot(int X, int Y)
	: GUIBase()
{	
	loadImage("Resources/Sprites/KitRobot.png");
	this->setX(X-10);
	this->setY(Y-70);
	this->setZValue(ZVALUE_KITROBOT);

	this->setRotationSpeed(10);

	hasKit = false;


	state = DoNothing;
	rotationSpeed=5;

	armLoc = QPointF(25,10);
	pivotLoc = QPointF(25, this->boundingRect().height()-25);
	this->setTransformOriginPoint(pivotLoc);


}

GUIKitRobot::~GUIKitRobot()
{
	// nothing to delete
}

void GUIKitRobot::DoBreak()
{
	GUIBase::DoBreak();

}

void GUIKitRobot::DoFix()
{
	GUIBase::DoFix();
	this->DoGoToNeutralPosition();
	qDebug()<<"FIXING";
}

// agent call to go to the original position
void GUIKitRobot :: DoGoToNeutralPosition()
{
	//qDebug() << "DoGoToNeutralPosition";

	state = DoNothing;
	bool clock=false;
	if(rotation()<30||rotation()>210)
		clock=true;
	rotateOverTime(20,clock);

}

// setting the agent
void GUIKitRobot :: setAgent(KitRobotAgent* agent)
{
	this->agent = agent;
	this->setGUIBaseAgent(agent);
}

// agent calls to go to the position either to get the kit or drop the kit
void GUIKitRobot ::DoGoToDestination(QPoint destination)
{

	qDebug() << "DoGoToDestination: "<<destination.x()<<","<<destination.y();
	state = Rotate;
	rotateTowardsOverTime(destination.x(), destination.y(), this->mapToScene(pivotLoc).x(), this->mapToScene(pivotLoc).y());

}

// agent calls to pick up the kit
void GUIKitRobot::DoPickUpKit(Kit *k)
{
	//qDebug() << "DoPIckUpKit";
	state = PickUpKit;
	hasKit = true;
	kitHeld=k;
	//kits[0]->scale(Kitscale_factor, Kitscale_factor);
}

// agent calls
void GUIKitRobot::DoDropOffKit()
{
	//qDebug() << "DoDropOffKit";
	state = DropOffKit;
	hasKit = false;
}

void GUIKitRobot::doneRotating()
{
	qDebug()<<"done Rotating!";
	agent->msgAnimationDone();
}


				
			

void GUIKitRobot::update()
{
	
	if(broken)
	{
		this->setRotation(this->rotation()+rotationSpeed);
	}
	else if(state == PickUpKit)
	{
		QPointF kitCoord = this->kitHeld->scenePos();
		qDebug()<<"Coords: "<<kitCoord.x()<<","<<kitCoord.y();
		kitHeld->setFlag(QGraphicsItem::ItemStacksBehindParent,true);
		kitHeld->setZValue(-1);
		//kitHeld->setTransformToCenter();
		kitHeld->setRotation(kitHeld->rotation()-this->rotation());
		kitCoord = this->mapFromScene(kitCoord);
		qDebug()<<"Coords: "<<kitCoord.x()<<","<<kitCoord.y();
		kitHeld->setParentItem(this);
		
		kitHeld->setPos(kitCoord.x(),kitCoord.y());
		

		
		qDebug()<<"Coords: "<<kitCoord.x()<<","<<kitCoord.y();

		agent->msgAnimationDone();
		state = DoNothing;
	 }

	else if(state == DropOffKit)
	{
		qDebug()<<"Kit Scene Coords: "<<kitHeld->scenePos().x()<<","<<kitHeld->scenePos().y();
		 QPointF kitCoord = this->mapToScene(kitHeld->pos());
		qDebug()<<"Drop Coords: "<<kitCoord.x()<<","<<kitCoord.y();
		 kitHeld->setParentItem(0);
		// kitHeld->setTransformToCenter();
		 kitHeld->setRotation(this->rotation()+kitHeld->rotation());
		 kitHeld->setFlag(QGraphicsItem::ItemStacksBehindParent,false);
		 kitHeld->setPos(kitCoord);
		 kitHeld->setZValue(ZVALUE_KIT);
		 kitHeld=NULL;

		 agent->msgAnimationDone();
		 state = DoNothing;
	}
 }


void GUIKitRobot:: advance(int phase)
{
	if(phase == 1)
	{
		rotateStep();
		if(movingInstantly)
			this->setPos(destination);
		if(rotatingInstantly){
			this->setRotation(targetRotation);
		}
		movingInstantly=false;
		rotatingInstantly=false;
		update();
		
	}
}

 